<!DOCTYPE html>
<!--
Copyright (c) 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/extras/chrome/cc/display_item_list.html">
<link rel="import" href="/tracing/ui/extras/chrome/cc/display_item_debugger.html">

<script>
'use strict';

// See https://crbug.com/1143376.
tr.b.unittest.skippedTestSuite(function() {
  test('instantiate', function() {
    const displayItemList = new tr.e.cc.DisplayItemListSnapshot(
        {id: '31415'},
        10,
        {
          'params': {
            'layer_rect': [-15, -15, 46, 833],
            'items': [
              'BeginClipDisplayItem',
              'EndClipDisplayItem'
            ]
          },
          'skp64': '[another skia picture in base64]'});
    displayItemList.preInitialize();
    displayItemList.initialize();

    const dbg = new tr.ui.e.chrome.cc.DisplayItemDebugger();
    this.addHTMLOutput(dbg);
    assert.isUndefined(dbg.displayItemList_);
    assert.isUndefined(dbg.picture_);
    dbg.displayItemList = displayItemList;
    assert.isDefined(dbg.displayItemList_);
    assert.isDefined(dbg.picture_);
    assert.strictEqual(dbg.displayItemList_.items.length, 2);
    dbg.style.border = '1px solid black';
  });

  test('selections', function() {
    const displayItemList = new tr.e.cc.DisplayItemListSnapshot(
        {id: '31415'},
        10,
        {
          'params': {
            'layer_rect': [-15, -15, 46, 833],
            'items': [
              'BeginClipDisplayItem',
              'TransformDisplayItem',
              {
                'name': 'DrawingDisplayItem',
                'skp64': '[skia picture in base64]',
              },
              'EndTransformDisplayItem',
              'EndClipDisplayItem'
            ]
          },
          'skp64': '[another skia picture in base64]'});
    displayItemList.preInitialize();
    displayItemList.initialize();

    const dbg = new tr.ui.e.chrome.cc.DisplayItemDebugger();
    this.addHTMLOutput(dbg);
    dbg.displayItemList = displayItemList;
    assert.isDefined(dbg.displayItemList_);
    assert.isDefined(dbg.picture_);
    assert.strictEqual(dbg.displayItemList_.items.length, 5);

    const initialPicture = dbg.picture_;
    assert.isAbove(initialPicture.guid, 0);

    // Select the drawing display item and make sure the picture updates.
    const listView = dbg.displayItemListView_;
    listView.selectedElement = listView.getElementByIndex(3);
    let updatedPicture = dbg.picture_;
    assert.isAbove(updatedPicture.guid, 0);
    assert.notEqual(initialPicture.guid, updatedPicture.guid);

    // Select the TransformDisplayItem and make sure the picture is blank.
    listView.selectedElement = listView.getElementByIndex(2);
    assert.isUndefined(dbg.picture_);

    // Deselect a list item and make sure the picture is reset to the original.
    listView.selectedElement = undefined;
    updatedPicture = dbg.picture_;
    assert.isAbove(updatedPicture.guid, 0);
    assert.strictEqual(initialPicture.guid, updatedPicture.guid);

    dbg.style.border = '1px solid black';
  });

  test('export', function() {
    const displayItemList = new tr.e.cc.DisplayItemListSnapshot(
        {id: '31415'},
        10,
        {
          'params': {
            'layer_rect': [-15, -15, 46, 833],
            'items': [
              'BeginClipDisplayItem',
              'EndClipDisplayItem'
            ]
          },
          'skp64': 'c2twaWN0dXJl'});
    displayItemList.preInitialize();
    displayItemList.initialize();

    const dbg = new tr.ui.e.chrome.cc.DisplayItemDebugger();
    this.addHTMLOutput(dbg);
    dbg.displayItemList = displayItemList;

    let onSaveDisplayListCalled = false;
    dbg.saveFile_ = function(filename, rawData) {
      onSaveDisplayListCalled = true;
      assert.strictEqual(filename, 'displayitemlist.json');
      assert.strictEqual(
          rawData, '["BeginClipDisplayItem","EndClipDisplayItem"]');
    };
    dbg.onExportDisplayListClicked_();
    assert(onSaveDisplayListCalled);

    let onSaveSkPictureCalled = false;
    dbg.saveFile_ = function(filename, rawData) {
      onSaveSkPictureCalled = true;
      assert.strictEqual(filename, 'skpicture.skp');
      assert.strictEqual(rawData, 'skpicture');
    };
    dbg.onExportSkPictureClicked_();
    assert(onSaveSkPictureCalled);
  });
});
</script>
